package com.gdut.multiusercommunicationsystem.controller;

import com.gdut.multiusercommunicationsystem.model.Friend;
import com.gdut.multiusercommunicationsystem.mapper.FriendMapper;
import com.gdut.multiusercommunicationsystem.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class FriendController {
    @Resource
    public FriendMapper friendMapper;

    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest httpServletRequest){
        HttpSession httpSession=httpServletRequest.getSession(false);
        if(httpSession==null){
            System.out.println("没有创建会话");
            return new ArrayList<Friend>();
        }
        User user=(User) httpSession.getAttribute("user");
        if(user==null){
            System.out.println("没有该用户");
            return new ArrayList<Friend>();
        }
        List<Friend> result=friendMapper.getFriendList(user.getUserId());
        return result;
    }
}